const db = require('../../db')

/**
 * POST 类别的回调函数
 * @param sortName  类别名称
 * @param sortcode  类别代码
 * @param sortType  类型
 * @param remark  备注
 */
exports.add = (req, res) => {
  // 接收表单的数据
  const sortInfo = req.body
  let sql = 'select * from account_sort where sortName=? and sortType=?'
  db.query(sql, [sortInfo.sortName, sortInfo.sortType], (error, result) => {
    if (error) {
      return res.cc(error)
    }
    if (result.length > 0) {
      return res.cc('类别名称已被占用!')
    }
    // 定义 SQL 语句
    let sql1 = 'insert into account_sort set ?'
    // 调用 db.query() 执行 sql 语句
    db.query(sql1, {
      sortName: sortInfo.sortName,
      sortType: sortInfo.sortType,
      remark: sortInfo.remark
    }, (error, result) => {
      if (error) return res.cc(error)
      // 判断影响行数是否为 1
      if (result.affectedRows !== 1) return res.cc('新增类别失败！')
      return res.cc('新增类别成功', true)
    })
  })
}

exports.del = (req, res) => {
  // 接收表单的数据
  const sortInfo = req.body
  let sql = 'select * from account_sort where sortId=?'
  db.query(sql, [sortInfo.sortId], (error, result) => {
    if (error) {
      return res.cc(error)
    }
    if (result.length === 0) {
      return res.cc('未找到该类别名称!')
    }
    // 定义 SQL 语句
    const sql1 = 'delete from account_sort where sortId=?'
    // 调用 db.query() 执行 sql 语句
    db.query(sql1, sortInfo.sortId, (error, result) => {
      if (error) return res.cc(error)
      // 判断影响行数是否为 1
      if (result.affectedRows !== 1) return res.cc('删除类别失败！')
      return res.cc('删除类别成功', true)
    })
  })
}

exports.update = (req, res) => {
  // 接收表单的数据
  const sortInfo = req.body
  let sql = 'select * from account_sort where sortId=?'
  db.query(sql, [sortInfo.sortId], (error, result) => {
    if (error) {
      return res.cc(error)
    }
    if (result.length === 0) {
      return res.cc('未找到该类别名称!')
    }
    // 定义 SQL 语句
    const sql1 = "update account_sort set sortName=?, sortType=?, remark=? where sortId=?";
    // 调用 db.query() 执行 sql 语句
    db.query(sql1, [sortInfo.sortName, sortInfo.sortType, sortInfo.remark, sortInfo.sortId], (error, result) => {
      if (error) return res.cc(error)
      // 判断影响行数是否为 1
      if (result.affectedRows !== 1) return res.cc('修改类别失败！')
      return res.cc('修改类别成功', true)
    })
  })
}

exports.allList = (req, res) => {
  const page = req.body.page ? parseInt(req.body.page) : 1; // Get the page number, default to the first page
  const limit = req.body.limit ? parseInt(req.body.limit) : 10; // Get the number of items per page, default to 10
  const sortName = req.body.sortName || ''; // Get sortName from request body or set to an empty string
  const sortType = req.body.sortType || ''
  const sqlCount = "SELECT COUNT(*) AS total FROM account_sort";
  db.query(sqlCount, (error, countResults) => {
    if (error) {
      return res.cc(error);
    }
    const total = countResults[0].total;
    const offset = (page - 1) * limit;
    let sql = `SELECT * FROM account_sort`;
    let params = [];
    if (sortName) {
      sql += ` WHERE sortName LIKE ?`;
      params.push(`%${sortName}%`);
    }
    if (sortType) {
      if (sortName) {
        sql += ` AND sortType = ?`;
      } else {
        sql += ` WHERE sortType = ?`;
      }
      params.push(sortType);
    }
    sql += ` LIMIT ? OFFSET ?`;
    params.push(limit, offset);
    db.query(sql, params, (error, results) => {
      if (error) {
        return res.cc(error);
      }
      return res.cc('获取类别成功', true, results, {
        total,
        page,
        limit
      });
    });
  });

}
